package cn.edu.scau.cmi.zhangxiyang.client;

import java.net.URL;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Scanner;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;

import cn.edu.scau.cmi.zhangxiyang.dao.StudentDAO;
import cn.edu.scau.cmi.zhangxiyang.dao.TeacherDAO;
import cn.edu.scau.cmi.zhangxiyang.domain.Student;
import cn.edu.scau.cmi.zhangxiyang.domain.Teacher;
import cn.edu.scau.cmi.zhangxiyang.dao.BaseHibernateDAO;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;

public class ComprehensionClient extends Application{
	
	public void start(Stage primaryStage) {
        try {
            Parent root = FXMLLoader.load(getClass().getClassLoader()
                    .getResource("cn/edu/scau/cmi/zhangxiyang/client/MyComprehensionClient.fxml"));

            primaryStage.setTitle("Hibernate");
            primaryStage.setScene(new Scene(root));
            primaryStage.show();

        } catch(Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        launch(args);
    }

	@FXML
	private Button AddStudent;
	@FXML
	private Button DelStudent;
	@FXML
	private Button ModifyStudent;
	@FXML
	private Button AddTeacher;
	@FXML
	private Button DelTeacher;
	@FXML
	private Button ModifyTeacher;
	@FXML
	private Button showStu;
	@FXML
	private Button showTea;
	@FXML
	private Button ChoseTutor;
	@FXML
	private Button ModifyTutor;
	@FXML
	private Button ChoseStu;
	@FXML
	private Button ModifyStu;

	@FXML
	private TextField StudentName;
	@FXML
	private TextField TeacherName;
	@FXML
	private TextArea show;
	@FXML
	private TextField StuName1;
	@FXML
	private TextField TeaName1;
	@FXML
	private TextField TeaName2;
	@FXML
	private TextField StuName2;
	@FXML
	private TextField newStuName;
	@FXML
	private TextField newTeaName;

	public void initialize(URL location, ResourceBundle resources) {
	       // TODO (don't really need to do anything here).
	}

	   // When user click on myButton
	   // this method will be called.
	public void AddStudent_Click(ActionEvent event) {
		String name = StudentName.getText();
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		Student student=new Student();
		student.setName(name);
		StudentDAO studentDAO=new StudentDAO();
		studentDAO.save(student);
		transaction.commit();
	}
	
	public void DelStudent_Click(ActionEvent event) {
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		String name = StudentName.getText();
		StudentDAO studentDAO = new StudentDAO();
		List stu = studentDAO.findByName(name);
		Iterator<?> studentIterator = stu.iterator();
		Student student = (Student) studentIterator.next();
		studentDAO.delete(student);
		transaction.commit();
	}
	
	public void ModifyStudent_Click(ActionEvent event) {
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		String name = StudentName.getText();
	    String newname = newStuName.getText();
		StudentDAO studentDAO = new StudentDAO();
		List stu = studentDAO.findByName(name);
		Iterator<?> studentIterator = stu.iterator();
		Student student = (Student) studentIterator.next();
		student.setName(newname);
		studentDAO.save(student);
		transaction.commit();
	}
	
	public void showStu_Click(ActionEvent event) {
		show.setText("");
		String a;
		StudentDAO studentDAO=new StudentDAO();
		List students = studentDAO.findAll();
		Iterator<?> studentIterator = students.iterator();
		while(studentIterator.hasNext()){
			Student student = (Student) studentIterator.next();
			if(student.getTeacher() != null) {
				a = "学生ID是："+student.getId()+"   学生姓名是："+student.getName()+"    导师是："+student.getTeacher().getName()+"\n";
			}
			else a = "学生ID是："+student.getId()+"   学生姓名是："+student.getName()+"    导师是：null"+"\n";
			show.appendText(a);
		}
	}
	
	public void AddTeacher_Click(ActionEvent event) {
		String name = TeacherName.getText();
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		Teacher Teacher=new Teacher();
		Teacher.setName(name);
		TeacherDAO TeacherDAO=new TeacherDAO();
		TeacherDAO.save(Teacher);
		transaction.commit();
	}
	
	public void DelTeacher_Click(ActionEvent event) {
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		String name = TeacherName.getText();
		TeacherDAO teacherDAO = new TeacherDAO();
		List tea = teacherDAO.findByName(name);
		Iterator<?> teacherIterator = tea.iterator();
		Teacher teacher = (Teacher) teacherIterator.next();
		teacherDAO.delete(teacher);
		transaction.commit();
	}
	
	public void ModifyTeacher_Click(ActionEvent event) {
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		String name = TeacherName.getText();
	    String newname = newTeaName.getText();
	    TeacherDAO teacherDAO = new TeacherDAO();
		List tea = teacherDAO.findByName(name);
		Iterator<?> teacherIterator = tea.iterator();
		Teacher teacher = (Teacher) teacherIterator.next();
		teacher.setName(newname);
		teacherDAO.save(teacher);
		transaction.commit();
	}
	
	public void showTea_Click(ActionEvent event) {
		show.setText("");
		TeacherDAO teacherDAO=new TeacherDAO();
		List teachers = teacherDAO.findAll();
		Iterator<?> teacherIterator = teachers.iterator();
		while(teacherIterator.hasNext()){
			Teacher teacher = (Teacher) teacherIterator.next();
			Set<Student> students = teacher.getStudents();
			String name = "";
			for(Student stu : students) {
				name += stu.getName() + " ";
			}
			//System.out.println("老师ID是："+teacher.getId()+"   老师姓名是："+teacher.getName()+"    学生有："+teacher.getStudents()+"\n");
			String a;
			if(students.isEmpty() == false) {
				a = "老师ID是："+teacher.getId()+"   老师姓名是："+teacher.getName()+"    学生有：["+ name +"]\n";
			}
			else a = "老师ID是："+teacher.getId()+"   老师姓名是："+teacher.getName()+"    学生有：[null]\n";
			show.appendText(a);
		}
	}

	public void ChoseTutor_Click(ActionEvent event) {
		String StuName = StuName1.getText();
		String TeaName = TeaName1.getText();
		Student student = new Student();
		Teacher teacher = new Teacher();
		StudentDAO studao = new StudentDAO();
		TeacherDAO teadao = new TeacherDAO();
		Set<Student> students = new HashSet<Student>(0);
		
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		
		List s = studao.findByName(StuName);
		List t = teadao.findByName(TeaName);
		Iterator<?> studentIterator = s.iterator();
		student = (Student) studentIterator.next();
		Iterator<?> teacherIterator = t.iterator();
		teacher = (Teacher) teacherIterator.next();
		
		student.setTeacher(teacher);
		students = teacher.getStudents();
		students.add(student);
		teacher.setStudents(students);
		studao.save(student);
		teadao.save(teacher);
		transaction.commit();
	}

	public void ModifyTutor_Click(ActionEvent event) {
		String StuName = StuName1.getText();
		String TeaName = TeaName1.getText();
		Student student = new Student();
		Teacher teacher_before = new Teacher();
		Teacher teacher_after = new Teacher();
		StudentDAO studao = new StudentDAO();
		TeacherDAO teadao = new TeacherDAO();
		Set<Student> students_after = new HashSet<Student>(0);
		Set<Student> students_before = new HashSet<Student>(0);
		
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		
		List s = studao.findByName(StuName);
		List t_after = teadao.findByName(TeaName);
		Iterator<?> studentIterator = s.iterator();
		student = (Student) studentIterator.next();
		Iterator<?> teacherIterator = t_after.iterator();
		teacher_after = (Teacher) teacherIterator.next();
		teacher_before = student.getTeacher();
		
		student.setTeacher(teacher_after);
		students_before.remove(student);
		students_after = teacher_after.getStudents();
		students_after.add(student);
		teacher_after.setStudents(students_after);
		teacher_before.setStudents(students_before);
		studao.save(student);
		teadao.save(teacher_after);
		teadao.save(teacher_before);
		transaction.commit();
	}

	public void ChoseStu_Click(ActionEvent event) {
		String TeaName = TeaName2.getText();
		String StuName = StuName2.getText();
		Student student = new Student();
		Teacher teacher = new Teacher();
		StudentDAO studao = new StudentDAO();
		TeacherDAO teadao = new TeacherDAO();
		Set<Student> students = new HashSet<Student>();
		
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		
		List s = studao.findByName(StuName);
		List t = teadao.findByName(TeaName);
		Iterator<?> studentIterator = s.iterator();
		student = (Student) studentIterator.next();
		Iterator<?> teacherIterator = t.iterator();
		teacher = (Teacher) teacherIterator.next();
		students = teacher.getStudents();
		students.add(student);
		teacher.setStudents(students);
		student.setTeacher(teacher);
		studao.save(student);
		teadao.save(teacher);
		transaction.commit();
	}

	public void ModifyStu_Click(ActionEvent event) {
		String TeaName = TeaName2.getText();
		String StuName = StuName2.getText();
		Student student = new Student();
		Teacher teacher = new Teacher();
		StudentDAO studao = new StudentDAO();
		TeacherDAO teadao = new TeacherDAO();
		Set<Student> students = new HashSet<Student>();
		
		Session session = BaseHibernateDAO.getSession();
		Transaction transaction = session.beginTransaction();
		
		List s = studao.findByName(StuName);
		List t = teadao.findByName(TeaName);
		Iterator<?> studentIterator = s.iterator();
		student = (Student) studentIterator.next();
		Iterator<?> teacherIterator = t.iterator();
		teacher = (Teacher) teacherIterator.next();
		
		students = teacher.getStudents();
		students.remove(student);
		teacher.setStudents(students);
		student.setTeacher(null);
		studao.save(student);
		teadao.save(teacher);
		transaction.commit();
	}

}